import os,random

from random import randint

import numpy as np


#正态分布
class normalGenerator:

    def __init__(self, mu, sigma):
        self.mu = mu #mu是正态分布的中心线左右移动
        self.sigma = sigma#sigma是正态分布宽窄的参数

    def gen(self, n, lb = None, rb = None):#指定范围lb-rb
        ret = np.random.normal(self.mu, self.sigma, n)
        for i in range(n):#检查超出范围的数据
            if lb is not None and ret[i]<lb:
                ret[i] = lb
            if rb is not None and ret[i]>rb:
                ret[i] = rb
        return ret

#报酬正太分布
def genPtsNormal(n,mu,umax,sigma=3.75):
    generator = normalGenerator(mu,sigma)
    lb = 0.01
    rb = umax
    pts = generator.gen(n,lb,rb)
    pts = pts.tolist()
    return pts

def genTask(longitude,latitude,rw):
    tloc_x = 0
    tloc_y = 0
    while True:#我们在以圆心为中心，以二倍半径为边长的正方形内部进行随机选点，当点落在圆内或者圆上时，满足条件则加入。
        deta_tloc_x = 1.0*randint(0,(2*rw)*10**6)/10**6
        deta_tloc_y = 1.0*randint(0,(2*rw)*10**6)/10**6
        radius = deta_tloc_x**2+deta_tloc_y**2
        tloc_x = round((deta_tloc_x + longitude),6)
        tloc_y = round((deta_tloc_y + latitude),6)
        if radius <= rw**2:
            break
    return tloc_x,tloc_y

def timeOrder(file):
    dataDict = {}
    with open("data_"+file+".txt","r")as f:
        for i in f.readlines():
            tempList = i.split()
            if len(tempList) == 4:
                # print(i)
                # print(i.split())
                firstStr=i
                workerNu=i.split()[0]
                taskNu = i.split()[1]
            else:
                dataDict.update({int(tempList[0]):tempList})
    data = sorted(dataDict)
    
    folderNu=[1,2,3,4,5,6,7,8,9,10,20]
    # folderNu=[1]
    for foNu in folderNu:
        destPath = "../EverySender_cap"+str(foNu)
        if not os.path.exists(destPath):
            os.mkdir(destPath)

        outputFileName = os.path.join(destPath,"data_"+file+"_time.txt")
        #修改总共节点数
        firstLine = firstStr.split()
        firstLine[2] = str(int(float(firstLine[2])))
        gentt = 5+foNu
        workerNu = int(firstLine[0])*gentt
        umax = int(firstLine[2])
        pts = genPtsNormal(workerNu,umax/2,umax)#生成任务报酬数据
        firstLine[3] = str(int(firstLine[0])*foNu+int(firstLine[1])+len(pts))#工作者能力乘以工作者数+全部任务数量
        print(workerNu)
        
        tempStr = ''
        for item in firstLine:
            tempStr +=item+' '
        tempStr+='\n'
        with open(outputFileName,'w') as f:
            f.write(tempStr)
            ii = 0
            for i in data:
                outstr = ''
                nodeType = dataDict[i][1]
                if nodeType=="w":
                    dataDict[i][5]=str(foNu)
                    random_number = random.randint(10, 10)
                    dataDict[i][6]=str(random_number)
                    outstr = str(ii)+' '
                    # ii +=1
                    for j in dataDict[i][1:]:
                        outstr +=j+' '
                    outstr+='\n'
                    f.write(outstr)
                    #根据工作者生成任务
                    for tti in range(gentt):
                        tlongitude,tlatitude = genTask(float(dataDict[i][2]),float(dataDict[i][3]),float(dataDict[i][4]))
                        outpt = round(pts.pop(),6)
                        outstr = f"{str(ii)} t {str(tlongitude)} {str(tlatitude)} {str(random_number)} {str(outpt)}\n"
                        f.write(outstr)
                        # ii += 1
                    ii += 1
                else:
                    random_number = random.randint(10, 10)
                    dataDict[i][4]=str(random_number)
                    outstr = str(ii)+' '
                    ii +=1
                    for j in dataDict[i][1:]:
                        outstr +=j+' '
                    outstr+='\n'
                    f.write(outstr)

if __name__=='__main__':
    # fileName=["00","01","02","03","04","05","06","07","08","09"]
    fileName=["00"]
    for i in fileName:
        timeOrder(i)
    # orderTime()